<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
            <label>选择文件的名字必须和导出表名一样</label>
            <input type="file" icon="ios-download-outline" @change="onFileChange">
            <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport" >导入全表Excel</Button>
            <br/>
            <label>表名:</label>
            <Select v-model="tableName" style="width:200px" not-found-text="-1">
              <Option v-for="item in tableList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
            <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出表EXCEL</Button>
        </form>
    </Row>
  </div>
</template>
<script>

import {importTables, getTableDatas} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      filename: '',
      results: [],
      tableName: 'active_event',
      count: 0,
      tableList: [
        {
          value: 'active_event',
          label: 'active_event'
        },
        {
          value: 'advertisement',
          label: 'advertisement'
        },
        {
          value: 'bet_level',
          label: 'bet_level'
        },
        {
          value: 'burst',
          label: 'burst'
        },
        {
          value: 'burst_need_jabe',
          label: 'burst_need_jabe'
        },
        {
          value: 'burst_probability',
          label: 'burst_probability'
        },
        {
          value: 'burst_status',
          label: 'burst_status'
        },
        {
          value: 'change_status',
          label: 'change_status'
        },
        {
          value: 'game_jackpot',
          label: 'game_jackpot'
        },
        {
          value: 'game_task',
          label: 'game_task'
        },
        {
          value: 'jion_match',
          label: 'jion_match'
        },
        {
          value: 'level',
          label: 'level'
        },
        {
          value: 'luck_pan',
          label: 'luck_pan'
        },
        {
          value: 'match',
          label: 'match'
        },
        {
          value: 'match_bouns',
          label: 'match_bouns'
        },
        {
          value: 'match_reward',
          label: 'match_reward'
        },
        {
          value: 'new_player_free',
          label: 'new_player_free'
        },
        {
          value: 'new_player_respin',
          label: 'new_player_respin'
        },
        {
          value: 'new_player_result',
          label: 'new_player_result'
        },
        {
          value: 'new_player_result_config',
          label: 'new_player_result_config'
        },
        {
          value: 'new_player_special',
          label: 'new_player_special'
        },
        {
          value: 'new_test_type',
          label: 'new_test_type'
        },
        {
          value: 'parameter_config',
          label: 'parameter_config'
        },
        {
          value: 'pay_line',
          label: 'pay_line'
        },
        {
          value: 'recharge_notice',
          label: 'recharge_notice'
        },
        {
          value: 'symbols',
          label: 'symbols'
        },
        {
          value: 'symbols_probability',
          label: 'symbols_probability'
        },
        {
          value: 'symbols_special',
          label: 'symbols_special'
        },
        {
          value: 'task',
          label: 'task'
        },
        {
          value: 'task_box',
          label: 'task_box'
        },
        {
          value: 'task_box_reward',
          label: 'task_box_reward'
        },
        {
          value: 'vip_status',
          label: 'vip_status'
        },
        {
          value: 'weekly_rank_reward',
          label: 'weekly_rank_reward'
        }
      ]
    }
  },
  created () {
  },
  methods: {
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    onImPortTables (tempData) {
      // 一直循环到表导光为止
      let len = this.results.length
      importTables(this.filename, tempData).then((data) => {
        if (!data) {
          return
        }

        if (this.count === len - 1) {
          let success = data.data.data.intValues[0]
          if (success === 1) {
            this.$Message.info('导入excel成功!!!')
          }
        } else {
          this.count = this.count + 1
          tempData = this.results[this.count]
          this.onImPortTables(tempData)
        }
      })
    },
    onImport () {
      // 导入EXCEL
      this.count = 0
      let tempData = this.results[0]

      this.onImPortTables(tempData)
    },
    onExport () {
      // 导出EXCEL
      getTableDatas(this.tableName).then((data) => {
        if (!data) {
          return
        }
        let allTableData = []
        let pv = data.data.data
        let len = pv.data.length

        let baseVO = null
        if (this.tableName === 'active_event') {
          baseVO = getVO('ActiveEventVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.id = '' + vo.id.toNumber()
            vo.startTime = '' + vo.startTime.toNumber()
            vo.endTime = '' + vo.endTime.toNumber()
            vo.closeTime = '' + vo.closeTime.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'advertisement') {
          baseVO = getVO('AdvertisementVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.id = '' + vo.id.toNumber()
            vo.endTime = '' + vo.endTime.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'bet_level') {
          baseVO = getVO('BetLevelVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.gameId = vo.gameId.toNumber()
            vo.money = '' + vo.money.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'burst') {
          baseVO = getVO('BurstVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'burst_need_jabe') {
          baseVO = getVO('BurstNeedJabeVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.needWinMoney = '' + vo.needWinMoney.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'burst_probability') {
          baseVO = getVO('BurstProbabilityVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'burst_status') {
          baseVO = getVO('BurstStatusVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'change_status') {
          baseVO = getVO('ChangeStatusVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'game_jackpot') {
          baseVO = getVO('GameJackPotVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.initBaseJackpot = '' + vo.initBaseJackpot.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'game_task') {
          baseVO = getVO('GameTaskVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'jion_match') {
          baseVO = getVO('JionMatchVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'level') {
          baseVO = getVO('LevelVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.id = '' + vo.id.toNumber()
            vo.exp = '' + vo.exp.toNumber()
            vo.addMoney = '' + vo.addMoney.toNumber()
            vo.addVipExp = '' + vo.addVipExp.toNumber()
            vo.activePoint = '' + vo.activePoint.toNumber()
            vo.addJabe = '' + vo.addJabe.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'luck_pan') {
          baseVO = getVO('LuckPanVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.weight = '' + vo.weight.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'match') {
          baseVO = getVO('MatchVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.loopTimes = '' + vo.loopTimes.toNumber()
            vo.longTimes = '' + vo.longTimes.toNumber()
            vo.cdTimes = '' + vo.cdTimes.toNumber()
            vo.rewardMoney = '' + vo.rewardMoney.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'match_bouns') {
          baseVO = getVO('MatchBounsVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.money = '' + vo.money.toNumber()
            vo.jion1PlayerNum = '' + vo.jion1PlayerNum
            vo.reward2MoneyRate = '' + vo.reward2MoneyRate
            allTableData.push(vo)
          }
        } else if (this.tableName === 'match_reward') {
          baseVO = getVO('MatchRewardVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.rewardMoneyPri = '' + vo.rewardMoneyPri.toNumber()
            vo.rewardMoneyTotalPri = '' + vo.rewardMoneyTotalPri.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'new_player_free') {
          baseVO = getVO('NewPlayerFreeVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'new_player_respin') {
          baseVO = getVO('NewPlayerRespinVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'new_player_result') {
          baseVO = getVO('NewPlayerResultVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'new_player_result_config') {
          baseVO = getVO('NewPlayerResultConfigVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'new_player_special') {
          baseVO = getVO('NewPlayerSpecialVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.indexId = '' + vo.indexId.toNumber()
            vo.special1 = '' + vo.special1.toNumber()
            vo.special2 = '' + vo.special2.toNumber()
            vo.special3 = '' + vo.special3.toNumber()
            vo.special4 = '' + vo.special4.toNumber()
            vo.special5 = '' + vo.special5.toNumber()
            vo.special6 = '' + vo.special6.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'new_test_type') {
          baseVO = getVO('NewTestTypeVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'parameter_config') {
          baseVO = getVO('ParameterConfigVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'parameter_config') {
          baseVO = getVO('ParameterConfigVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        } else if (this.tableName === 'pay_line') {
          baseVO = getVO('PlayLineVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.gameId = '' + vo.gameId.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'recharge_notice') {
          baseVO = getVO('RechargeNoticeTMVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.points = '' + vo.points.toNumber()
            vo.jade = '' + vo.jade.toNumber()
            vo.addVIPPoints = '' + vo.addVIPPoints.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'symbols') {
          baseVO = getVO('SymbolsVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.gameId = '' + vo.gameId.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'symbols_probability') {
          baseVO = getVO('SymbolsProbabilityVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.gameId = '' + vo.gameId.toNumber()
            vo.element1Rate = '' + vo.element1Rate.toNumber()
            vo.element2Rate = '' + vo.element2Rate.toNumber()
            vo.element3Rate = '' + vo.element3Rate.toNumber()
            vo.element4Rate = '' + vo.element4Rate.toNumber()
            vo.element5Rate = '' + vo.element5Rate.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'symbols_special') {
          baseVO = getVO('SymbolsSpecialVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.gameId = '' + vo.gameId.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'task') {
          baseVO = getVO('TaskVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.taskId = '' + vo.taskId.toNumber()
            vo.gameId = '' + vo.gameId.toNumber()
            vo.rewardPoint = '' + vo.rewardPoint.toNumber()
            vo.rewardJade = '' + vo.rewardJade.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'task_box') {
          baseVO = getVO('TaskBoxVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.boxId = '' + vo.boxId.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'task_box_reward') {
          baseVO = getVO('TaskBoxRewardVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.rewardId = '' + vo.rewardId.toNumber()
            vo.num = '' + vo.num.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'vip_status') {
          baseVO = getVO('VipStatusVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            vo.id = '' + vo.id.toNumber()
            vo.needExp = '' + vo.needExp.toNumber()
            allTableData.push(vo)
          }
        } else if (this.tableName === 'weekly_rank_reward') {
          baseVO = getVO('WeeklyRankRewardVO')
          for (let i = 0; i < len; i++) {
            let vo = baseVO.decode(pv.data[i])
            allTableData.push(vo)
          }
        }

        console.log(allTableData)
        exportExcel(this.tableName, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      let arr = e.target.files[0].name.split('.')
      this.filename = arr[0]
      console.log('arr:', arr)
      console.log('filename:', this.filename)
      importExcel(e.target, this.filename, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
